package cn.yunyichina.provider.bed.service.impl;

import cn.yunyichina.provider.bed.thread.OutpatientMedicineRecordRunnable;
import cn.yunyichina.provider.bed.thread.OutpatientSummaryRunnable;
import cn.yunyichina.provider.bed.dao.OutpatientMedicineRecordDao;
import cn.yunyichina.provider.bed.dao.OutpatientMedicineRecordListDao;
import cn.yunyichina.provider.bed.dao.OutpatientSummaryDao;
import cn.yunyichina.provider.bed.vo.BedVo;
import cn.yunyichina.provider.framework.dubbo.service.HisifaceService;
import cn.yunyichina.provider.iface.entity.common.ResCommon;
import cn.yunyichina.provider.iface.entity.common.ResponseObject;
import cn.yunyichina.provider.iface.entity.hisiface.bed.*;
import cn.yunyichina.utils.convert.JsonUtils;
import com.alibaba.dubbo.common.utils.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * Created by zd on 2016/12/12 0012.
 * 出院功能
 */
@Service("outbedServiceImpl")
public class OutBedServiceImpl {
    private static final Logger logger = LoggerFactory.getLogger(OutBedServiceImpl.class);

    @Autowired
    private OutpatientSummaryDao outpatientSummaryDao;
    @Autowired
    private OutpatientMedicineRecordDao outMedicineRecordDao;
    @Autowired
    private OutpatientMedicineRecordListDao outMedicineRecordListDao;

    public ResponseObject getOutBedMedicine(String data) {
        logger.info("getOutBedMedicine data: "+data);
        ResponseObject response = new ResponseObject();
        BedVo requestVo = JsonUtils.parseObject(data, BedVo.class);

        if (StringUtils.isBlank(requestVo.getHospitalCode())||StringUtils.isBlank(requestVo.getPatientId())||StringUtils.isBlank(requestVo.getAdmissionNo())||StringUtils.isBlank(requestVo.getInTime())){
            response.setResultCodeAndMessage("14002","hospitalCode,住院记录ID,住院号，住院次数 不得为空");
            return response;
        }


        try {
            OutMedicineVo outMedicineVo = new OutMedicineVo();
            outMedicineVo.setHospitalCode(requestVo.getHospitalCode());
            outMedicineVo.setAdmissionNo(requestVo.getAdmissionNo());
            outMedicineVo.setPatientId(requestVo.getPatientId());
            outMedicineVo.setInTime(requestVo.getInTime());
            ResCommon<OutMedicineVoResponse> outMedicineVoResponse = HisifaceService.getHisifaceClient().getOutMedicine(outMedicineVo);
            if (!"0".equals(outMedicineVoResponse.getResultCode())){
                response.setResultCodeAndMessage("14998", outMedicineVoResponse.getResultMessage());
                return response;
            }

            OutMedicineVoResponse outMedicine = outMedicineVoResponse.getResult();

            Thread outMedicineRecordRunnable = new Thread(new OutpatientMedicineRecordRunnable(outMedicine,requestVo));
            outMedicineRecordRunnable.start();

            response.setResultCodeAndMessageAndResult("0","成功",outMedicine);

        } catch (Exception ex) {
            response.setResultCodeAndMessage("14999"," 系统异常");
            return  response;
        }


        return response;
    }

    public ResponseObject getOutBedSummary(String data) {

        ResponseObject response = new ResponseObject();
        BedVo requestVo = JsonUtils.parseObject(data, BedVo.class);
        logger.info("getOutBedSummary origin data ={} , converter requestVo ={}",new Object[]{data,JsonUtils.toJsonString(requestVo)});
        if (StringUtils.isBlank(requestVo.getHospitalCode())||StringUtils.isBlank(requestVo.getPatientId())||StringUtils.isBlank(requestVo.getAdmissionNo())||StringUtils.isBlank(requestVo.getInTime())){
            response.setResultCodeAndMessage("14002","hospitalCode,住院记录ID,住院号，住院次数 不得为空");
            return response;
        }


        try {
            OutSummaryVo outSummaryVo = new OutSummaryVo();
            outSummaryVo.setHospitalCode(requestVo.getHospitalCode());
            outSummaryVo.setAdmissionNo(requestVo.getAdmissionNo());
            outSummaryVo.setPatientId(requestVo.getPatientId());
            outSummaryVo.setInTime(requestVo.getInTime());
            ResCommon<OutSummaryVoResponse> outSummaryVoResponse = HisifaceService.getHisifaceClient().getOutSummary(outSummaryVo);
            if (!"0".equals(outSummaryVoResponse.getResultCode())){
                response.setResultCodeAndMessage("14998", outSummaryVoResponse.getResultMessage());
                return response;
            }

            OutSummaryVoResponse outSummary = outSummaryVoResponse.getResult();
            //开启线程保存出院小结
            Thread threadOutpatientSummary = new Thread(new OutpatientSummaryRunnable(outSummary,requestVo));
            threadOutpatientSummary.start();
            logger.info("getOutBedSummary return data ={} ",new Object[]{JsonUtils.toJsonString(outSummary)});
            response.setResultCodeAndMessageAndResult("0","成功",outSummary);

        } catch (Exception ex) {
            ex.printStackTrace();
            response.setResultCodeAndMessage("14999"," 系统异常");
            return  response;
        }

        return response;
    }

    /**
     * 出院待缴费记录查询
     * yyt.bed.outbed.settlement.fee.get
     * @param data
     * @param sessionId
     * @return
     */
    public ResponseObject getOutBedSettlementFee(String data, String sessionId){
        //定义返回结果
        ResponseObject response = new ResponseObject();
        BedSettlementFeeVoResponse bedFee = new BedSettlementFeeVoResponse();

        //定义返回码和返回信息
        String resultCode = "0";
        String resultMessage = "执行成功";

        try {
            //获取入参
            logger.info("【出入院-出院待缴费记录】开始, 请求="+data,",sessionId=" + sessionId);
            BedVo bedVo = JsonUtils.parseObject(data, BedVo.class);

            //检查参数合法性
            boolean check = true;
            if(StringUtils.isBlank(bedVo.getAdmissionNo()) && StringUtils.isBlank(bedVo.getPatientId())){
                check = false;
                resultCode = "14002";
                resultMessage = "参数有误:" + "admission_no住院号和patient_id住院记录ID不能同时为空";
            }

            //检查不通过，结束流程
            if(!check){
                return response;
            }

            BedSettlementFeeVo bedFeeVo = new BedSettlementFeeVo();
            bedFeeVo.setHospitalCode(bedVo.getHospitalCode());
            bedFeeVo.setPatientId(bedVo.getPatientId());
            bedFeeVo.setAdmissionNo(bedVo.getAdmissionNo());
            //不能写死 add by zd
            bedFeeVo.setInTime(bedVo.getInTime());
            ResCommon<BedSettlementFeeVoResponse> bedFeeVoResponse = HisifaceService.getHisifaceClient().getOutSettlementFee(bedFeeVo);

            //如果访问his失败
            if (!"0".equals(bedFeeVoResponse.getResultCode())){
                response.setResultCodeAndMessage(bedFeeVoResponse.getResultCode(),bedFeeVoResponse.getResultMessage());
            }else{
                bedFee = bedFeeVoResponse.getResult();
            }
        } catch (Exception e) {
            resultCode = "14999";
            resultMessage = "系统异常:" + e.getMessage();
            e.printStackTrace();
        } finally {
            //组装返回结果
            response.setResultCodeAndMessageAndResult(resultCode, resultMessage, JsonUtils.toJsonString(bedFee));
            logger.info("【出入院-出院待缴费记录】结束, 返回结果=" + JsonUtils.toJsonString(response));
            return response;
        }
    }

}
